package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SetmealMapper {

    /**
     * 根据分类id查询套餐的数量
     *
     * @param id
     * @return
     */
    @Select("select count(id) from setmeal where category_id = #{categoryId}")
    Integer countByCategoryId(Long id);

    /**
     * 新增套餐
     *
     * @param setmeal
     */
    @AutoFill(OperationType.INSERT)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into setmeal(category_id, name, price, status, description, image, create_time, update_time, create_user, update_user) " +
            "       values(#{categoryId},#{name},#{price},#{status},#{description},#{image},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    void insert(Setmeal setmeal);

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    Page<SetmealDTO> selectByPage(SetmealPageQueryDTO setmealPageQueryDTO);

    /**
     * 查询 ids对应的套餐中，起售状态的套餐 数量
     * @param ids
     * @return
     */
    int selectEnanbleSetmealsCount(List<Long> ids);

    /**
     * 根据ids批量删除套餐
     * @param ids
     */
    void batchDeleteByIds(List<Long> ids);

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Select("select * from setmeal where id = #{id}")
    Setmeal selectById(Integer id);

    /**
     * 修改套餐
     * @param setmeal
     */
    @AutoFill(OperationType.UPDATE)
    void updateById(Setmeal setmeal);


    /**
     * 根据套餐id查询菜品
     * @param setmealId
     * @return
     * 根据套餐id查询菜品id 根据菜品id查询菜品
     */
    @Select("select * from dish where id in (select dish_id from setmeal_dish where setmeal_id = #{setmealId} )")
    List<Dish> selectBySetmeamlId(Long setmealId);

    @Select("select * from setmeal where category_id = #{categoryId} and status = 1")
    List<Setmeal> selectEnableListByCategoryId(Integer categoryId);

    @Select("select count(*) from setmeal where status = #{status}")
    Integer countByStatus(Integer status);
}
